package com.itheima.service.impl;

import com.itheima.controller.Code;
import com.itheima.dao.BookDao;
import com.itheima.domain.Book;
import com.itheima.exception.SystemException;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Xiao
 * @date 2021/9/16
 */
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;
    @Override
    public Boolean save(Book book) {
        return bookDao.save(book) > 0;
    }
    @Override
    public Boolean update(Book book) {
        return bookDao.update(book) > 0;
    }
    @Override
    public Boolean delete(Integer id) {
        return bookDao.delete(id) > 0;
    }
    @Override
    public Book selectById(Integer id) {
        //将可能出现的异常进行包装，转换成自定义异常
        if (id < 1) {
            throw new SystemException(Code.BUSINESS_ERR,"请不要使用你的技术挑战我的耐性？！");
        }
        try {
//            int i = 1 / 0;
            return bookDao.selectById(id);
        } catch (Exception e) {
            throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"服务器访问超时，请重试。。。", e);
        }

    }
    @Override
    public List<Book> selectAll() {
        return bookDao.selectAll();
    }
}
